﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ADDDF.Infrastructure.Specifications
{
    public class SpecificationList
    {
        private List<ISpecification> _sl;
        private List<Object> _ol;
        private int _failedSpecIdx;

        public SpecificationList()
        {
            _sl = new List<ISpecification>();
            _ol = new List<object>();
        }

        public SpecificationList Add(ISpecification specification, object obj)
        {
            _sl.Add(specification);
            _ol.Add(obj);
            return this;
        }

        public bool IsSatisfied()
        {
            _failedSpecIdx = -1;

            for (int i = 0; i < _sl.Count; i++)
            {
                if (!_sl[i].IsSatisfiedBy(_ol[i]))
                {
                    _failedSpecIdx = i;
                    break;
                }
            }

            return _failedSpecIdx == -1;
        }

        public ISpecification FailedSpecification
        {
            get { return _sl[_failedSpecIdx]; }
        }

        public Object FailedObject
        {
            get { return _ol[_failedSpecIdx]; }
        }
    }

    public static class SLBuilder
    {
        public static SpecificationList Add(ISpecification specification, object obj)
        {
            SpecificationList sl = new SpecificationList();
            return sl.Add(specification, obj);
        }
    }
}
